<?php 
require(dirname(dirname(__FILE__))."/Config/config.php");
require(dirname(dirname(__FILE__))."/jssdk.php");
Class shakeController extends appController
{
	public $prize1 = 10000;
	public $prize2 = 5000;
	public $prize3 = 4000;
	public $prize4 = 3000;
	public $prize5 = 2000;
	public $playnums = 5;
	public $joinnums = 10000;
	public $wxid = 0;
	public $appid = "wx7ac5aa35b3f2a17c";
	public $secret = "18cd5300b2c096c7be629e4a9072e2c0";
	public $s;
	public $m;
	public $info=array();
	public $record = "nv7_record";
	public $join = "nv7_join";
	public $total = "nv7_total";
	
	
	public function __construct()
	{
		
		parent::__construct();
		//微信分享参数构建
		$this->s = Model('star');
// 		$this->appid=$this->s->is_appid();
// 		$this->secret=$this->s->is_ser();
		
		$jssdk = new JSSDK($this->appid, $this->secret);
		
		$signPackage = $jssdk->GetSignPackage();
			
		$this->assign('signPackage',$signPackage);
		
		
		//基础信息构建
		$this->m = Model('shake');
		
		//当前用户的微信id
		$this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		
		if($this->wxid>0)
		{
			//当前用户的微信信息
			
			$this->info = $this->m->get_record($this->record,$this->wxid);
			$this->assign('info',$this->info);
		}
		
		$path=SUBDIR."/Home/Webroot/default/";
		
		$option['hy_path'] = $path;
		
		$this -> assign('_C',$option);
		
		$this -> assign('vision',$_SERVER['PHP_SELF']);
		
// 		$this -> assign('vision',$_SERVER['PHP_SELF']);
		
	}
	
	//需要授权才可进入游戏
	public function canIn()
	{
		if(!$this->info)
		{
			header("Location:http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=index");
			exit;
		}
	}
	
	
	public function goResult()
	{
		
// 		date_default_timezone_set('PRC');//使用PHP的date函数获取时间之前，先将时区设置为北京时区
		
// 		if( time() >strtotime('2015-08-31 22:00:00'))
// 		{
			
// 			header("Location:http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=result");
// 		}
		
			
	}
	//清除数据
	public function clear2()
	{
		
		unset($_SESSION['wxid']);
		unset($_SESSION['pid']);
		
		echo "已清除数据";
		exit;
	}
	
	
	public function index3()
	{
		
// 		echo $_SERVER['REQUEST_URI'];
// 		var_dump($_SERVER);
		$this->display("index3.html");
	}
	
	
	
	
	
	
	public function index4()
	{
		/* //分享者的用户id
		$pid = $_GET['pid']?$_GET['pid']:0;
		//是否通过他人分享链接进入游戏
		if($pid>0)
		{
			$_SESSION['pid'] = $pid;
		} */
		
		
		//未授权成功 或 SESSION 已过期
	   if(!isset($_SESSION['wxid']))
	   {
		   	
	   		if(!$_POST['openid'])
	   		{
	   			header("Location:http://nvidia.mmxchina.com/epolice_interface/index.aspx?a_type=18");
	   			exit;
	   		}
	   		
	   		$var = array("openid","nickname","sex","language","province","city","country","headimgurl");
	   	
	   		$arr = post_var($var);
	   		
	   		
	   		//构建微信用户信息
	   		$arr = $this->m->buildArr($arr);
	   		
   				
   				$this->wxid=$this->s->is_user_existed($arr['openid']);
   				
   				//是否首次进入游戏
   				if(!$this->wxid)
   				{
   					$this->wxid = $this->s->insertUser($arr);
   				}
   				else 
   				{

   					$this->s->updateUser($arr['headimgurl'],$this->wxid);
   				}
   				
   				//授权成功，设置SESSION，并重新进入
   				$_SESSION['wxid'] = $this->wxid;
   				
   				
		   		header('Location:http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?m=shake&g=home&a=index');
		   		
		   		
		 }
		else
	   {
	   	
	   		
	   	  //已成功授权并SESSION未过期
	   	  
	   		$data['wxid'] = $_SESSION['wxid'];
	   	
			 $this->m->create_record($this->record,$data);
			
	   		if(!$_SESSION['wxid'])
	   		{
	   			header("http://nvidia.mmxchina.com/epolice_interface/index.aspx?a_type=18");
	   			exit;
	   		}
	   		
			//自己通过自己链接进入，无效
	   		if($_SESSION['wxid'] == $_SESSION['pid'] )
	   		{
	   			$pid = 0;
	   			
	   		}
	   		else 
	   		{
	   			$pid = $_SESSION['pid'];
	   		}
	   			
	   		unset($_SESSION['pid']);
	   		
	   		//分享人的信息
	   		$info2 = $this->m->get_record($this->record,$pid );
	   		
	   		
	   		//是否通过他人分享的链接首次进入游戏 并且 分享人仍具有助力资格
	   		if($pid>0 && $pid !=$_SESSION['wxid'] && $info2['joinnums'] <$this->joinnums  && $info2 )
	   		{
	   			
	   			$where = " where wxid = ".$data['wxid'];
	   			 
	   			$join['pid'] = $pid;
	   				
	   			$join['wxid'] =$data['wxid'];
	   			
	   			//构建一条助力join记录
	   			$re = $this->m->create_join($this->join,$join,$where);
	   			
	   			if($re)
	   			{
	   				
	   				$where = " where wxid = ".$join['pid'];
	   				
	   				$this->m->setInc($this->record,"joinnums",$where,1);
	   			}
	   		
	   			
	   		}
	  
	        $this->display('index.html');
	   }
	}
	
	
	


	public function index()
	{
		//分享者的用户id
		$pid = $_GET['pid']?$_GET['pid']:0;
		//是否通过他人分享链接进入游戏
		if($pid>0)
		{
			$_SESSION['pid'] = $pid;
		}
	
		
		//未授权成功 或 SESSION 已过期
		if(!isset($_SESSION['wxid']))
		{
			
				// 是否有code
				if(!isset($_GET['code']))
			   	   {
			   	   	$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			   	   	$url=$this->parseurl($url);
		
			     	header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
			 	
			     	}
		    	else 	   		
		   	  	{
		   	  		
		   	  		// 返回code
		   	  		$weiXinObj= new weixinController();
		   	  		
			   		$code = $_GET['code'];
					
			   		// 获取openid 和 access_token
			   		$back=$weiXinObj->getUserByCode($this->appid,$this->secret,$code);
			   		
		
					$access_token=$back['access_token'];
					$openid=$back['openid'];
					
					if($openid)
					{
						
						// 获取微信用户信息
						$arr=$weiXinObj->getUserInfo($openid,$access_token);
						
						
						//该用户是否首次进入
						$this->wxid=$this->s->is_user_existed($openid);
							
						//是否首次进入游戏
						if(!$this->wxid)
						{
							
							//构建微信用户信息
							$arr = $this->m->buildArr($arr);
							
							$this->wxid = $this->s->insertUser($arr);
						}
						else
						{
							//更新头像
							$this->s->updateUser($arr['headimgurl'],$this->wxid);
						}
							
						
						//授权成功，设置SESSION，并重新进入
						$_SESSION['wxid'] = $this->wxid;
						
					}
					
				
					header('Location:http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?m=shake&g=home&a=index');
		   		
				
				 
				 
			}
		}
		else
		{
	
	
			//已成功授权并SESSION未过期
			 
			$data['wxid'] = $_SESSION['wxid'];
		  
			$this->m->create_record($this->record,$data);
				
	
			//自己通过自己链接进入，无效
			if($_SESSION['wxid'] == $_SESSION['pid'] )
			{
				$pid = 0;
		   
			}
			else
			{
				$pid = $_SESSION['pid'];
			}
			 
			unset($_SESSION['pid']);
	
			//分享人的信息
			$info2 = $this->m->get_record($this->record,$pid );
	
	
			//是否通过他人分享的链接首次进入游戏 并且 分享人仍具有助力资格
			if($pid>0 && $pid !=$_SESSION['wxid'] && $info2['joinnums'] <$this->joinnums && $info2['prize'] ==0 && $info2 )
			{
		   
				$where = " where wxid = ".$data['wxid'];
	
				$join['pid'] = $pid;
	
				$join['wxid'] =$data['wxid'];
		   
				//构建一条助力join记录
				$re = $this->m->create_join($this->join,$join,$where);
		   
				if($re)
				{
	
					$where = " where wxid = ".$join['pid'];
	
					$this->m->setInc($this->record,"joinnums",$where,1);
				}
	
		   
			}
			 
			$this->display('index.html');
		}
	}
	
	//模拟SESSION赋值，用于测试
	public function set()
	{
		$_SESSION['wxid']=$_GET['wxid'];
	}
	
	//进入游戏后，首先判断是否仍具有游戏资格   异步
	public function canrun()
	{
		
		//1首次玩  2非首次玩且已分享且少于规定游戏次数 3上次游戏未中断  
		if(($this->info['playnums']<1 || ($this->info['playnums']<$this->playnums&&$this->info['is_share']==1)) && $this->info['pre'] ==1)
		{
			
			$data['pre'] = 0;
			
			$back = $this->m->update_record($this->record,$data,$this->wxid);
			
			die($this->info['step']);

		}
		else 
		{
			die("error");
		}
	}
	
	
	//个人信息收集
	public function person()
	{
		
// 		$rank = $_REQUEST['rank']?$_REQUEST['rank']:0;
		
		
		$this->canIn();
		
		//非法进入 或 未兑奖
// 		if($rank==0 && $this->info['prize'] ==0)
// 		{
// 			header("Location:http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=shake&a=prize");
// 			exit;
// 		}
		
// 		$this->assign("rank", $rank);
		$this->display('person.html');
	}
	
	
	
	//提交个人信息，异步
	public function submitInfo()
	{
		
		$this->canIn();
		
		$data['name'] = $_POST['name'];
		$data['phone'] = $_POST['phone'];
		$data['address'] = $_POST['address'];

		$back = $this->m->update_record($this->record,$data,$this->wxid);
		
		echo "ok";
		
// 		$rank = $_POST['rank']?$_POST['rank']:0;
		
		
// 		if($this->wxid > 0 && $rank>0)
// 		{
			
				
// 			$score = $this->info['score']+$this->info['add_score'];
				
// 			if(($rank==1 && $score>=$this->prize1) || ($rank==2 && $score>=$this->prize2) || ($rank==3 && $score>=$this->prize3 ) || ($rank==4 && $score>=$this->prize4) || ($rank==5 && $score>=$this->prize5))
// 			{
// 				$data['name'] = $_POST['name'];
// 				$data['phone'] = $_POST['phone'];
// 				$data['address'] = $_POST['address'];
// 				$data['prize'] = $_POST['rank'];

// 				$back = $this->m->update_record($this->record,$data,$this->wxid);
				
// 				echo "ok";
// 			}
// 			else
// 			{
// 				echo('你的积分不够。');
// 			}
				
			
			
// 		}
// 		else 
// 		{
// 			echo "页面已过期。";
// 		}
		
// 		exit;
	}
	
	// 是否足够积分兑换 异步
	public function canChange()
	{
		
		$rank = $_POST['rank']?$_POST['rank']:0;
		
		if($this->wxid > 0)
		{
			
			$score = $this->info['score']+$this->info['add_score'];
			
			if(($rank==1 && $score>=$this->prize1) || ($rank==2 && $score>=$this->prize2) || ($rank==3 && $score>=$this->prize3 ) || ($rank==4 && $score>=$this->prize4) || ($rank==5 && $score>=$this->prize5))
			{
				die('ok');
			}
			else 
			{
				die('error');
			}
			
			
		}
		else
		{
			echo "页面已过期";
		}
	
		exit;
	}
	
	
	//一轮游戏结束处理逻辑
	public function operaGame()
	{
// 		$this->goResult();
		
// 		die("非法操作");
		
		 $joiner = $this->m->get_join($this->join,$this->wxid);
			
		$data['pre'] = 1 ;
			
		//是否首次游戏	
		if($this->info['playnums'] ==0)
		{
		
			$data['step'] = 1 ;
		
			$data['playnums'] = $this->info['playnums'] +1;
		
			$pid = $joiner['pid']>0?$joiner['pid']:0;
			
			//为分享者加分
			if($pid>0)
			{
					
				$where = " where wxid = ".$pid;
		
				$this->m->setInc($this->record,"add_score",$where,$this->info['score']);
		
				$join['score'] = $this->info['score'];
					
				$this->m->update_join($this->join,$join,$this->wxid);
			}
		
		}
		//是否最后一次游戏
		else if($this->info['playnums'] == $this->playnums-1 )
		{
			$data['playnums'] = $this->info['playnums'] +1;
			$data['step'] = 9 ;
		}
		else if($this->info['playnums'] > $this->playnums-1 )
		{
			$data['playnums'] = $this->info['playnums'];
			$data['step'] = 9 ;
		}
		else
		{
			$data['playnums'] = $this->info['playnums'] +1;
			$data['step'] = 1 ;
		}
			
		// 			$where = " where wxid = ".$wxid;
			
		// 			$this->m->setInc($this->record,"playnums",$where,1);
			
		$this->m->update_record($this->record,$data,$this->wxid);
			
			 
			
	}
	
	//时间到，游戏结束  异步
	public function endGame()
	{
// 		$this->goResult();
		
// 		die("非法操作");
		
		 if($this->wxid > 0)
		{
			
			$this->operaGame();
			
			if($this->info['playnums'] > $this->playnums-1 )
			{
			
				die("已超过游戏次数。");
			}
			
			echo "ok";
		}
		else
		{
			echo "页面已过期";
		}
	
		exit; 
	}
	
	
	//挑战关卡成功，提交分数  异步
	public function submitScore()
	{
		
// 		$this->goResult();
		
		if($_POST['score'] > 1500 || $this->info['playnums'] > $this->playnums-1)
		{
			echo "非法操作";
			exit;
		}
		
// 		die("非法操作");
		
	 	if($this->wxid > 0)
		{
			
			
			$step = $_POST['step'];
			$second = $_POST['score'];
			
			$score = $this->m->get_score($step,$second);
			
			$data['score'] =$this->info['score']+ $score;
			
			$data['pre'] = 1;
			
			
			//是否首次玩
			if($this->info['playnums']==0)
			{
				//是否第八关
				if($step ==8)
				{
					
					$data['step'] = 1;
					
					$data['playnums'] = $this->info['playnums'] +1;
					
					$joiner = $this->m->get_join($this->join,$this->wxid);
					
					$pid = $joiner['pid']>0?$joiner['pid']:0;
					
					if($pid>0  )
					{
						$where = " where wxid = ".$pid;
								
						$this->m->setInc($this->record,"add_score",$where,$data['score']);
						
						$join['score'] = $data['score'];
							
						$this->m->update_join($this->join,$join,$this->wxid);
					}
					
				}
				else 
				{
					$data['step'] = $step +1;
				}
			}
			//是否最后一次玩 
			else if($this->info['playnums'] == $this->playnums-1 )
			{
				//是否第八关
				if($step ==8)
				{
					$data['step'] = 9 ;
					
					$data['playnums'] = $this->info['playnums'] +1;
					
				}
				else
				{
					$data['step'] = $step +1;
				}
				
			}
			//是否超过游戏次数
			else if($this->info['playnums'] > $this->playnums-1 )
			{
					
				die("已超过游戏次数。");
			}
			else
			{
				//非首次 非最后一次 非超次数
				if($step ==8)
				{
					$data['playnums'] = $this->info['playnums'] +1;
					$data['step'] = 1 ;
				}
				else
				{
					$data['step'] = $step +1;
				}
			}
			
			
			//更新record记录
			$back = $this->m->update_record($this->record,$data,$this->wxid);
				
			echo $score;
		}
		else
		{
			echo "页面已过期";
		}
	
		exit; 
	}
	
	
	
	//分享朋友圈或好友 异步
	public function share()
	{
		
		
		if($this->wxid==0)
		{
			die("error");
		}
		
		$data['is_share'] = 1 ;
		
		
		
	    $this->m->update_record($this->record,$data,$this->wxid);
		
		die("ok");
		
	}
	
	//查看游戏结果
	public function result()
	{
		
			
			$this->canIn();
			
			$this->display("result.html");
	}
	
	//个人中心
	public function me()
	{
	
		
			
		$this->canIn();

		$joins = $this->m->get_joins($this->join,$this->wxid);
		
		$this->assign("joins", $joins);

		$this->display("me.html");
	}
	
	
	//游戏规则
	public function rules()
	{
	
		
		$this->display("rules.html");
	}
	
	//进入兑奖
	public function prize()
	{
	
		$this->canIn();
			
		$this->display("prize.html");
	}
	
	//游戏排名
	public function rank()
	{
		
		$records = $this->m->get_records($this->record,20);

		$this->assign("records", $records);

		$this->display("rank.html");
	}
	
	
	//关卡挑战成功提示
	public function msg()
	{
		
		$this->canIn();
			
		$this->display("msg.html");
	}
	
	
	//进入游戏
	public function game()
	{
// 		$this->goResult();
		
// 		die("非法操作");
		
		$this->canIn();
		
		//上次游戏是否中断
		if($this->info['pre']==0)
		{
			//未超过游戏次数
			if($this->info['playnums'] <= $this->playnums-1 )
			{
				$this->operaGame();
			}
			
// 			header('Location:http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?m=shake&g=home&a=index');
// 			echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=result";
// 			exit;
			header("Location:http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=result");
			exit;
		}
		
		//已兑换奖品
// 		if($this->info['prize'] >0)
// 		{
// 			header("Location:http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=shake&a=person");
// 			exit;

// 		}
		
// 		elseif($this->info['playnums']>=$this->playnums && $this->info['prize'] ==0 )
// 		{
// 			header("Location:http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=shake&a=result");
// 			exit;
// 		}
		//已玩过且未分享
		elseif($this->info['playnums']==1 && $this->info['is_share'] ==0)
		{
			header("Location:http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=result");
			exit;
			
		}
		//已超过游戏次数
		elseif($this->info['playnums']>=$this->playnums || $this->info['step']>8)
		{
			header("Location:http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=home&m=shake&a=me");
			exit;
			
		}
		
		//游戏模板
		$tpl = "game".$this->info['step'];
		
		//前台随机模板
		$ran = mt_rand(1, 3);
		
		$this->assign("ran", $ran);
		
		$this->display($tpl.".html");
	}
	
	function parseurl($url="")
	{
		$url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));
	
		return $url;
	}
}